package com.bstek.controller;

import com.bootdo.clouddocommon.annotation.Log;
import com.bootdo.clouddocommon.utils.PageUtils;
import com.bootdo.clouddocommon.utils.Query;
import com.bootdo.clouddocommon.utils.R;
import com.bstek.model.domain.Employee;
import com.bstek.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @ClassName EmployeeController
 * @Description 员工信息交互
 * @Author fazi
 * @Date 2019/1/9 19:57
 * @Version 1.0
 **/
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    EmployeeService employeeService;

    @Log("获取员工列表")
    @GetMapping()
    R listByPage(@RequestParam Map<String, Object> params) {
        Query query = new Query(params);
        List<Employee> employees = employeeService.list(query);
        int total = employeeService.count(query);
        PageUtils pageUtil = new PageUtils(employees, total);
        return R.ok().put("page",pageUtil);
    }

    /**
     * 增加用户
     * @param employee
     * @return
     */
    @PostMapping()
    R save(@RequestBody Employee employee) {
        return R.operate(employeeService.save(employee) > 0);
    }

    /**
     * 修改用户
     * @param user
     * @return
     */
    @PutMapping()
    R update(@RequestBody Employee user) {
        int num = employeeService.update(user);
        return R.operate(num > 0);
    }


    /**
     * 删除用户
     * @param username
     * @return
     */
    @DeleteMapping()
    R remove( String username) {
        return R.operate (employeeService.remove(username) > 0);
    }
}
